package com.wtf.dsp.admin.core.provider;

import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider;
import com.baomidou.dynamic.datasource.provider.YmlDynamicDataSourceProvider;
import lombok.Data;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * description: 动态数据源配置提供类
 * date: 2022/6/18 10:59
 * @author linhuanyou
 * @email 496558736@qq.com
 * @version 1.0
 */
@Data
public class DynamicDataSourceConfigProvide implements ApplicationContextAware {
    protected ApplicationContext applicationContext;
    protected DynamicRoutingDataSource dynamicRoutingDataSource;
    protected YmlDynamicDataSourceProvider ymlDynamicDataSourceProvider;
    protected DefaultDataSourceCreator defaultDataSourceCreator ;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
        this.dynamicRoutingDataSource = (DynamicRoutingDataSource) applicationContext.getBean(DataSource.class);
        DynamicDataSourceProvider dynamicDataSourceProvider = applicationContext.getBean(DynamicDataSourceProvider.class);
        this.ymlDynamicDataSourceProvider = (YmlDynamicDataSourceProvider) dynamicDataSourceProvider;
        this.defaultDataSourceCreator = (DefaultDataSourceCreator) applicationContext.getBean(DefaultDataSourceCreator.class);
    }
}
